<?php
namespace Shop\Controller;
use Common\Controller\AdminbaseController;
class FormatController extends AdminbaseController{
    
    protected $data_model;
    
    function _initialize(){
       parent::_initialize();
       $this->data_model = D('Format');
    }
    
    public function index(){
        $data = $this->data_model->order("`order` ASC")->select();
        $this->assign("data", $data);
        $this->display();
    }
    
    public function add()
    {
        $this->display();
    }
    
    public function add_post()
    {
        if (IS_POST) {
            if ($this->data_model->create() !== false) {
                if ($this->data_model->add() !== false) {
                    // F('all_terms',null);
                    $this->success("添加成功！", U("Format/index"));
                } else {
                    $this->error("添加失败！");
                }
            } else {
                $this->error($this->data_model->getError());
            }
        }
    }
    
    public function delete()
    {
        if (I('get.id')) {
            $id = I('get.id');
            $res = $this->data_model->delete($id);
            if ($res) {
                $this->success("删除成功！");
            } else {
                $this->error("删除失败！");
            }
        }
        if (I('post.ids')) {
            $ids = I('post.ids');
            $res = $this->data_model->where(array(
                "id" => array(
                    "in",
                    $ids
                )
            ))->delete();
            if ($res) {
                $this->success("批量删除成功！");
            } else {
                $this->error("批量删除失败！");
            }
        }
    }
    
    public function edit()
    {
        $data = $this->data_model->find(I('get.id'));
        $this->assign("data", $data);
        $this->display();
    }
    
    public function edit_post()
    {
        $floor_arr = $_POST;
        $res = $this->data_model->save($floor_arr);
        if ($res) {
            $this->success("编辑成功！");
        } else {
            $this->error($this->data_model->getError());
        }
    }
}